import 'dart:async';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/user.dart';
import 'package:qgchat/http/models/appointmentListModel.dart';

class CallBloc implements BlocBase {

  int page = 0;
  int limit = 2;
  List<AppointmentListModel> list = [];
  bool isFirstLoading = true; // 是否第一次加载

  CallBloc() {
    getMake();
  }

  // ##########  DEFAULT  ##############

  final List<AppointmentListModel> callDefault = [];

  // ##########  STREAMS  ##############

  /// 预约stream
  StreamController<List<AppointmentListModel>> _call = StreamController.broadcast();
  Sink<List<AppointmentListModel>> get _callSink => _call.sink;
  Stream<List<AppointmentListModel>> get callStream => _call.stream;

  void dispose(){
    _call.close();
  }

  // ##########  HANDLING  ##############

  /// 获取预约列表数据
  void getMake() async {
    page = page + 1;
    List<AppointmentListModel> result = await UserApis.appointmentList(page: page, limit: limit);
    if (result.isNotEmpty) {
      isFirstLoading = false;
      list.addAll(result);
      _callSink.add(list);
    } else {
      if (!isFirstLoading) showToast("已经没有了喔~ww");
      isFirstLoading = false;
    }
  }

  /// 刷新预约列表
  void onRefresh() async {
    page = 0;
    list = [];
    getMake();
  }
}